package org.skyui.controllers;

import org.skyui.beanmanagers.AbstractBeanManager;
import org.skyui.beanmanagers.BeanRelationManager;
import org.skyui.beanmanagers.BeanSetManager;
import org.skyui.beanmanagers.CommandManager;
import org.skyui.beans.AbstractBean;
import org.skyui.config.BeanCreateViewConfig;
import org.skyui.config.BeansTableViewConfig;
import org.skyui.views.AbstractBeansTableView;
import org.skyui.views.BeanCreateView;
import org.skyui.views.BeansTablePopupSelectView;
import org.skyui.views.ExistingBeansTableView;
import org.skyui.views.AbstractBeansTableView.Action;

import com.baycrypt.vaadinui.VaadinUIApplication;
import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.Button.ClickEvent;

public abstract class AbstractBeansTableController extends AbstractController {

	protected AbstractBeanManager beanManager;
	AbstractBeansTableView view;

	public AbstractBeansTableController(AbstractBeanManager beanManager,
			AbstractBeansTableView view) {
		this.beanManager = beanManager;
		this.view = view;
	}

	public void doAdd(ClickEvent event) {
		String addItemCaption;
		if (this.beanManager instanceof BeanRelationManager) {
			addItemCaption = ((BeanRelationManager) this.beanManager)
					.getAddItemCaption();
		} else {
			addItemCaption = BeansTableViewConfig
					.getAddItemCaption((BeanSetManager) this.beanManager);
		}
		final Window subwindow = new Window(addItemCaption);

		VerticalLayout layout = (VerticalLayout) subwindow.getContent();
		layout.setMargin(true);
		layout.setSpacing(true);
//		layout.setSizeFull();
		if (this.beanManager instanceof BeanRelationManager) {
			subwindow.setWidth("600px");
			BeansTablePopupSelectView tb = new BeansTablePopupSelectView(
					((BeanRelationManager) this.beanManager).getGlobalManager(),
					this.view.getContainer(),
					(BeanRelationManager) this.beanManager);
			subwindow.addComponent(tb);
		} else {
			subwindow.setWidth("600px");
			AbstractBean bi = ((BeanSetManager) this.beanManager)
					.createTemplate();
			BeanCreateView st = new BeanCreateView("", "", bi,
					VaadinUIApplication.getInstance(),
					(ExistingBeansTableView) this.view,
					(BeanSetManager) this.beanManager);
			subwindow.addComponent(st);
		}
		this.view.getWindow().addWindow(subwindow);
		subwindow.center();
	}

	public void doCommand(ClickEvent event) {

		BeanSetManager commandManager = (BeanSetManager) this.beanManager
				.getCommandManagers().get(event.getButton().getCaption());
		final Window subwindow;
		subwindow = new Window(BeansTableViewConfig
				.getAddItemCaption(commandManager));

		VerticalLayout layout = (VerticalLayout) subwindow.getContent();
		layout.setMargin(true);
		layout.setSpacing(true);
		//layout.setSizeFull();
		subwindow.setWidth("400px");
		AbstractBean b = this.view.getSelectedBean();
		if (b == null)
			return;
		AbstractBean bi = ((CommandManager) commandManager).createTemplate(b);
		BeanCreateView st = new BeanCreateView("", "", bi, VaadinUIApplication
				.getInstance(), (ExistingBeansTableView) this.view,
				commandManager);
		st.setActionButtonName(BeanCreateViewConfig
				.getActionButtonName(commandManager));
		subwindow.addComponent(st);
		this.view.getWindow().addWindow(subwindow);
		subwindow.center();
	}

	public void doRemove(ClickEvent event) {
		AbstractBean b = this.view.getSelectedBean();
		if (b != null) {
			removeAction(b);
			this.view.removeSelectedBean();
			this.view.getWindow().showNotification("Deleted OK");
		}

	}

	protected abstract void removeAction(AbstractBean b);

	public void buttonClick(ClickEvent event) {
		Action action = this.view.getAction(event.getButton().getCaption());
		if (action == null)
			return;
		if (action == Action.ADD) {
			doAdd(event);
		} else if (action == Action.REMOVE) {
			doRemove(event);
		} else if (action == Action.COMMAND) {
			doCommand(event);
		}

	}

}
